package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    /**
     * 新增订单
     * @param orders
     */
    @Insert(" insert into orders\n" +
            "        (number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark,\n" +
            "         phone, address, consignee, estimated_delivery_time, delivery_status, pack_amount, tableware_number,\n" +
            "         tableware_status)\n" +
            "        values (#{number}, #{status}, #{userId}, #{addressBookId}, #{orderTime}, #{checkoutTime}, #{payMethod},\n" +
            "                #{payStatus}, #{amount}, #{remark}, #{phone}, #{address}, #{consignee},\n" +
            "                #{estimatedDeliveryTime}, #{deliveryStatus}, #{packAmount}, #{tablewareNumber}, #{tablewareStatus})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    void save(Orders orders);
    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 根据支付状态和下单时间查询订单
     * @param payStatus
     * @param orderTime
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time = #{orderTime}")
    List<Orders> findByLongTime(Integer status, LocalDateTime orderTime);

    /**
     * 根据配送状态和下单时间查询订单
    * */
    @Select("select * from orders where status = #{status} and order_time = #{orderTime}")
    List<Orders> findByDeliveryOrder(Integer status, LocalDateTime orderTime);

    /*
    * 用户订单数据查询
    * */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /*
    * 查询订单详情
    * */
    @Select("select * from order_detail where order_id = #{orderId}")
    List<OrderDetail> findByOrderId(Long orderId);

    /*
    * 根据id查询订单
    * */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    /*
    * 查询订单总营业额
    * */
    Double sumByMap(Map map);

    /*
    * 查询订单数量
    * */
    Integer countByMap(Map map);

    /*
    * 查询订单销量前10
    * */
    List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin,LocalDateTime end);
}
